package app.netant.finishbook.txt2017;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class Txt2017Detail extends NetAntBase {
	
	private Txt2017Entity entity;
	
	private String exp;

	public Txt2017Detail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(Txt2017Entity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select(".headertop #navbot #pageMain .crumb .crumbleft a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】："+type);
			this.entity.setNovelType(type);
		}
		
	}
	
	public void analyseTitle(){
		Elements activePart = document.select(".headertop #navbot #pageMain #mainBody .pageMainArea .downInfoTitle h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}
	
	public void analyseTags(){
		Elements activePart = document.select(".headertop #navbot #pageMain #mainBody .pageMainArea #downInfoArea table tbody tr td dd.downInfoRowR");
		if(activePart.size()>0){
			String tagAlls = activePart.get(0).text();
			
			System.out.println(tagAlls);
			
			String[] tagm = tagAlls.split("：");
			String keys = "";
			String vals = "";
			
			for(int i=0;i<tagm.length;i++){
				
				String tmp = tagm[i].trim();
				
				if(i==0){
					keys += "," + tmp.replaceAll(" ", "");
				}
				else if(i==tagm.length-1){
					vals += "," + tmp.replaceAll(" ", "");
				}
				else{
					
					int first = tmp.indexOf(" ");
					if(first>0){
						keys += "," + tmp.substring(first).replaceAll(" ", "");
						vals += "," + tmp.substring(0,first).replaceAll(" ", "");
					}
					
				}
				
			}
			
			if(!"".equals(keys)){
				keys = keys.substring(1);
				vals = vals.substring(1);
			}
			
			String[] keyS = keys.split(",");
			String[] valS = vals.split(",");
			
			for(int j=0;j<keyS.length;j++){
				
				System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
				
				if("文件类型".equals(keyS[j])){
					this.exp = valS[j];
				}
				else if("电子书名".equals(keyS[j])){
					this.entity.setNovelName(valS[j]);
				}
				
				this.entity.setTag(keyS[j], valS[j]);
				
			}	
		}
	}
	
	/**
	 * 解析简介
	 */
	public void analyseMemo(){
		
		Elements activePart = document.select(".headertop #navbot #pageMain #mainBody .pageMainArea .mainstory .story #mainSoftIntro");
		if(activePart.size()>0){
			String memo = this.getString(activePart.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
		
	}
	
	/**
	 * 解析地址
	 */
	public void analyseHref(){
		
		Elements activePart = document.select(".headertop #navbot #pageMain #mainBody .pageMainArea .mainstory .story #downAddress a");
		if(activePart.size()>0){
			String click = activePart.get(0).attr("onclick");
			
			int startIdx = click.indexOf("/e/DownSys");
			int endIdx = click.indexOf("pathid=0");
			
			if(startIdx >0 && endIdx >0 && endIdx > startIdx){
				
				String href = click.substring(startIdx,endIdx+8);
				
				href = "https://www.txt2017.com" + href;
				
				System.out.println("【OpenUrl】："+href);
				
				Txt2017Detail detail = new Txt2017Detail(href);
				String stepUrl = detail.getStepUrl();
				
				System.out.println("【Href】:"+stepUrl);
				
				this.entity.setDownPath(stepUrl);
				
				String name = this.entity.getNovelName();
				this.entity.setNovelFullName(name+this.exp);
				
			}
			
			
		}
		
	}
	
	public String getStepUrl(){
		
		Elements activePart = document.select("table tbody tr td a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			if(href.startsWith("..")){
				href = "https://www.txt2017.com/e/DownSys" + href.substring(2);
				return href;
			}
		}
		
		throw new RuntimeException("无法获取跳转路径");
		
	}
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}

	public static void main(String[] args) {
		
		Txt2017Detail detail = new Txt2017Detail("https://www.txt2017.com/yq/26602.html");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		detail.analyseMemo();
		detail.analyseHref();
		
	}
	
}
